package com.sofly.view;

import java.io.OutputStream;
import java.util.List;  
import java.util.Map;  
  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractExcelView;  
  
import  com.sofly.entity.Suppliers;
  
/**
 * 
* <p>Title:RefuelingRecordExcelView </p>
* <p>Description: </p>
* <p>Company: </p> 
* @author ChengShiMin
* @date 下午7:36:29
 */
@Component
public class RefuelingRecordExcelView extends AbstractExcelView {

	@Override
	protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String fileName = "供应商信息表.xlsx";  
        response.setCharacterEncoding("UTF-8");  
        response.setContentType("application/ms-excel");  
        response.setHeader("Content-Disposition", "inline; filename="+new String(fileName.getBytes(),"iso8859-1"));  
        OutputStream outputStream = response.getOutputStream();
        
        @SuppressWarnings("unchecked")
		List<Suppliers> suppliersList = (List<Suppliers>) model.get("suppliersList");        
        HSSFSheet sheet = workbook.createSheet("供应商信息表");
        sheet.setDefaultColumnWidth(20);
        HSSFRow header = sheet.createRow(0);
        header.createCell(0).setCellValue("供应商编号");
        header.createCell(1).setCellValue("供应商名称");
        header.createCell(2).setCellValue("电话");
        header.createCell(3).setCellValue("地址");
        header.createCell(4).setCellValue("描述");
        header.createCell(5).setCellValue("信誉等级");
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("mm/dd/yyyy"));
        int rowNumber = 1;
        for (Suppliers suppliers : suppliersList) {
            HSSFRow row = sheet.createRow(rowNumber++);            
            row.createCell(0).setCellValue(suppliers.getSupplierId());
            row.createCell(1).setCellValue(suppliers.getSupplierName());
            row.createCell(2).setCellValue(suppliers.getPhone());
            row.createCell(3).setCellValue(suppliers.getAddress());
            row.createCell(4).setCellValue(suppliers.getDesc());
            row.createCell(5).setCellValue(suppliers.getLevel());
        }
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
		
	}  
  
     
}  